<?php
declare (strict_types = 1);

namespace app\service;

use EasyWeChat\Factory;
use EasyWeChat\OfficialAccount\Application as OfficialAccount;
use EasyWeChat\Payment\Application as Payment;
use think\facade\Config;

class WechatService
{
    /**
     * 获取微信公众号实例
     *
     * @return OfficialAccount
     */
    public static function officialAccount()
    {
        $config = Config::get('wechat.official_account.default');
        return Factory::officialAccount($config);
    }

    /**
     * 获取微信支付实例
     *
     * @return Payment
     */
    public static function payment()
    {
        $config = Config::get('wechat.payment.default');
        return Factory::payment($config);
    }

    /**
     * 获取用户信息
     *
     * @param string $code OAuth授权码
     * @return array
     */
    public static function getUserInfo($code)
    {
        $app = self::officialAccount();
        $oauth = $app->oauth;
        
        // 使用授权码获取访问令牌
        $accessToken = $oauth->getAccessToken($code);
        
        // 使用访问令牌获取用户信息
        $user = $oauth->user($accessToken);
        
        return $user->toArray();
    }
} 